<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/11/24
 * Time: 14:15
 */

namespace User\Model;


use Common\Model\CommonModel;

class AdminLevel extends CommonModel
{
    protected $connection = 'DB_CONFIG2';
    protected $trueTableName = 'cmf_admin_level';

    public function getTreeAll(){
        $result = $this
            ->field('admin_id as id,admin_id,pid,level')
            ->order('path asc')
            ->select();
        //讲所有管理员查出来
        $admins = M('users')->where(['user_status'=>1])->getField('id,user_login');
        array_walk($result,function(&$v,$k ) use ($admins) {
            $v['name'] = $admins[$v['id']];
            $v['pname'] = $admins[$v['pid']];
        });
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $n=> $r) {
            $prefix = '└─';
            $result[$n]['name'] = str_repeat('&nbsp;&nbsp;&nbsp;',$r['level']).$prefix.$r['name'];
            $result[$n]['parentid_node'] = ($r['pid']) ? ' class="child-of-node-' . $r['pid'] . '"' : '';
            $result[$n]['style'] = empty($r['pid']) ? '' : 'display:none;';
            $result[$n]['str_manage'] = '<a href="' . U("indexadmin/add_admin", array("id" => $r['id'], "admin_id" => I("get.admin_id"))) . '">添加下级管理员</a> | <a href="' . U("indexadmin/edit_admin_level", array("id" => $r['id'], "admin_id" => I("get.admin_id"))) . '">编辑</a>';
        }
        $tree->init($result);
        $str = "<tr id='node-\$id' \$parentid_node style='\$style'>
					<td style='padding-left:20px;'></td>
        			<td>\$name</td>
					<td>\$pname</td>
					<td>\$str_manage</td>
				</tr>";
        $categorys = $tree->get_tree(0, $str);
        return $categorys;
    }

    /**
     * @use          [取出当前管理员ID的所有的父级管理员ID]
     * @author       chenxiaogang
     * @param $id
     * @return array
     */
    public function getAllPids($id){
        static $pids = [];
        $pid = $this->where('admin_id='.$id)->getField('pid');
        if((int)$pid!==0){
            $pids = array_merge($pids,$this->getAllPids($pid));
        }
        $pids[] = $pid;
        return $pids;
    }

    /**
     * @use          [提取当前管理员ID的所有的子管理员ID]
     * @author       chenxiaogang
     * @param $all      [所有
     * @param $id       [被找的
     * @return array
     */
    public function getAllSonIds($all,$id){
        $arr = array();
        foreach($all as $v){
            if($v['pid'] == $id){
                $arr[] = $v['admin_id'];
                $arr = array_merge($arr,$this->getAllSonIds($all,$v['admin_id']));
            }
        };
        return $arr;
    }

    /**
     * @use          [SQL的方式查出自己子级ID列表]
     * @author       chenxiaogang
     * @param $pid
     * @return array
     */
    public function getSonIds($pid)
    {
        $where = "FIND_IN_SET({$pid},REPLACE(REPLACE(path,CONCAT('-',SUBSTRING_INDEX(path,'-',-1)),''),'-',','))";
        $data = $this->where($where)->order('path asc')->getField('admin_id',true);
        return $data;
    }


    /**
     * @use          [对一套ids构建树形option]
     * @author       chenxiaogang
     * @param $ids  array|string
     * @return string
     */
    public function buildOptionTreeByIds($ids)
    {
        $model = new AdminLevel();
        //将所有管理员查出来
        $result = $model->field('admin_id as id,admin_id,pid,level')->where(['admin_id'=>['in',$ids]])->order('path asc')->select();
        //将对应要用到的用户名称找出
        $admins = M('users')->where(['user_status'=>1,'id'=>['in',$ids]])->getField('id,user_login');
        $tree = new \Tree();
        foreach ($result as $k => $r) {
            $prefix = '└─';
            $result[$k]['name'] = str_repeat('&nbsp;&nbsp;&nbsp;',$r['level']) . $prefix .$admins[$r['id']];
        }
        $str = "<option value='\$id'>\$name</option>";
        $tree->init($result);
        return $tree->get_tree(0, $str);
    }

}